import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {
    AlertController, Content, IonicPage, LoadingController, NavController, NavParams,
    ToastController
} from 'ionic-angular';
import {WebcastSearchListProvider} from "../../../providers/webcast/webcast-search-list/webcast-search-list.provider";
import {DateService} from "../../../services/common/date.service";
import * as moment from "moment";
import {ClientListProvider} from "../../../providers/client/client-list/client-list.provider";

@IonicPage()
@Component({
    selector: 'page-webcast-search-list',
    templateUrl: 'webcast-search-list.html',
    providers: [
        WebcastSearchListProvider,
        DateService
    ]
})
export class WebcastSearchListPage {
    /*列表数据*/
    private searchResultData: Array<any> = [];

    /*搜索关键字*/
    private keyWord: string;

    //默认每次从第一页展示，每页最多展示20条
    private readonly pageNum: number = 1;
    private readonly pageSize: number = 1;
    private defaultBgImgUrl: string = './assets/images/webcast/default-live-small.png';
    moment = moment;

    isScrolling = false;
    isArriveBottom = false;
    @ViewChild(Content) content: Content;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public alertCtrl: AlertController,
                public toastCtrl: ToastController,
                public loadCtr: LoadingController,
                private dateService: DateService,
                public changeRef: ChangeDetectorRef,
                private webCastSearchListProvider: WebcastSearchListProvider) {
    }

    ionViewDidLoad() {
        this.keyWord = this.navParams.get('keyWord') || '';
        this.getMessageList();
    }

    // 获取消息列表
    getMessageList() {
        let loading = this.loadCtr.create({
            spinner: 'circles',
            content: "加载中",
        });
        loading.present();
        this.webCastSearchListProvider.getSearchResultList(this.keyWord, this.pageNum, this.pageSize).subscribe((videoList) => {
            videoList.forEach((item) => {
                if (item.type === 2) {
                    //预约
                    item.webcastStartTime = this.dateService.formatCastDate(item.webcastStartTime);
                } else {
                    item.webcastStartTime = this.moment(item.webcastStartTime * 1000).format('YYYY/MM/DD');
                }
                let imgUrl = item.webcastImgUrl;
                item.webcastImgUrl = imgUrl ? imgUrl : this.defaultBgImgUrl;//设置默认图片
            });
            this.searchResultData = videoList;
            loading.dismiss();
        }, () => {
            loading.dismiss();
        });
    }

    /*跳转收藏夹*/
    goCollectionList() {
        this.navCtrl.push('WebcastFavoriteListPage');
    }

    /*搜索按钮*/
    searchHandle() {
        this.getMessageList();
    }

    /*enter搜索*/
    inputKeyUp(e) {
        if (('Enter' === e.key || 13 === e.keyCode)) {
            this.getMessageList();
        }
    }

    /*下拉刷新*/
    doRefresh(refresher) {
        this.getMessageList();
        setTimeout(() => {
            refresher.complete();
        }, 1000);
    }

    // 到消息详情
    seeVideoDetail(item) {
        this.navCtrl.push('WebcastDetailPage', {
            castId: item.id,
            castTitle: item.subject,
            roomId: item.webinarId
        });
    }

    /**
     * 提示框
     */
    showToast(msg) {
        let toast = this.toastCtrl.create({
            message: msg,
            position: 'middle',
            duration: 1200
        });
        toast.present();
        toast.dismissAll();
    }

    //滚动相关
    scrollStart() {
        this.isScrolling = true;
    }

    scrollEnd() {
        this.isScrolling = false;
    }

    scrolling() {
        const dimensions = this.content.getContentDimensions();
        const temp1 = dimensions.scrollHeight - dimensions.scrollTop - dimensions.contentHeight < 100;
        if (temp1 !== this.isArriveBottom) {
            this.isArriveBottom = temp1;
            this.changeRef.detectChanges();
        }
    }

    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }
}
